package com.mate.cloud.xml.dtd;

import com.mate.cloud.xml.AbstractTestCase;
import org.dom4j.dtd.InternalEntityDecl;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * Tests the {@link InternalEntityDecl}functionality. Tests each of the
 * property access methods and the serialization mechanisms. Correct parsing is
 * tested by {@link DocTypeTest}.
 *
 * @author: MI
 * @email: 448341911@qq.com
 * @createTime: 2024/6/3 15:26
 * @updateUser: MI
 * @updateTime: 2024/6/3 15:26
 * @updateRemark: 修改内容
 * @version: v1.0
 */
@SpringBootTest
public class InternalEntityDeclTests extends AbstractTestCase {
    // Test case(s)
    // -------------------------------------------------------------------------
    @Test
    public void testToString() {
        InternalEntityDecl decl1 = new InternalEntityDecl("name", "value");
        InternalEntityDecl decl2 = new InternalEntityDecl("%name", "value");

        assertEquals("<!ENTITY name \"value\">", decl1.toString());
        assertEquals("<!ENTITY % name \"value\">", decl2.toString());
    }

    /**
     * Tests parameter entity declaration, such as
     *
     * <pre>
     *
     *
     *   &lt;!ENTITY % boolean &quot;( true | false )&quot;&gt;
     *
     *
     * </pre>
     * <p>
     * Note: There is a required whitespace between the "%" and the name of the
     * entity. This whitespace is required in the declaration and is not allowed
     * when writing a reference to the entity, e.g., "%boolean;" is a legal
     * reference but not "% boolean;".
     *
     * <p>
     * Note: The "%" is part of the parameter entity name as reported by the SAX
     * API. See <a href="http://tinyurl.com/6xe9y">LexicalHandler </a>, which
     * states:
     *
     * <pre>
     *
     *
     *   General entities are reported with their regular names,
     *   parameter entities have '%' prepended to their names, and the
     *   external DTD subset has the pseudo-entity name &quot;[dtd]&quot;.
     *
     *
     * </pre>
     *
     * </p>
     */
    @Test
    public void testParameterEntity() {
        String expectedName = "%boolean";

        String expectedValue = "( true | false )";

        String expectedText = "<!ENTITY % boolean \"( true | false )\">";

        InternalEntityDecl actual = new InternalEntityDecl(expectedName, expectedValue);

        assertEquals("name is correct", expectedName, actual.getName());

        assertEquals("value is correct", expectedValue, actual.getValue());

        assertEquals("toString() is correct", expectedText, actual.toString());
    }

    /**
     * Tests general entity declaration, such as:
     *
     * <pre>
     *
     *
     *   &lt;!ENTITY foo &quot;bar&quot;&gt;
     *
     *
     * </pre>
     */
    @Test
    public void testGeneralEntity() {
        String expectedName = "foo";

        String expectedValue = "bar";

        String expectedText = "<!ENTITY foo \"bar\">";

        InternalEntityDecl actual = new InternalEntityDecl(expectedName, expectedValue);

        assertEquals("name is correct", expectedName, actual.getName());

        assertEquals("value is correct", expectedValue, actual.getValue());

        assertEquals("toString() is correct", expectedText, actual.toString());
    }

}
